﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Studio.Share.Serialization;
using DWF.Studio.Share.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DWF.Studio.ScriptEngine.Custom
{
    internal static class BreakpointsHandler
    {
        public const string BreakpointSetting = "settings.yml";

        public static IEnumerable<Breakpoint> GetBreakpoints(string projectPath, string filePath)
        {
            List<Breakpoint> breakpoints = null;

            var setting = getSetting(projectPath);
            if (setting.Breakpoints == null)
            {
                setting.Breakpoints = new List<Breakpoint>();
            }
            else
            {
                breakpoints = setting.Breakpoints.Where(item => item.Path == filePath).ToList();
            }

            return breakpoints;
        }

        private static BreakpointSetting getSetting(string projectPath)
        {
            var settingPath = System.IO.Path.Combine(projectPath, BreakpointSetting);

            BreakpointSetting setting = null;
            if (FileHelper.CheckFileExisting(settingPath))//检查配置文件是否存在，如果存在这加载，否者新建。
            {
                setting = YamlHelper.Load<BreakpointSetting>(settingPath, true);
            }

            if (setting == null)
            {
                setting = new BreakpointSetting();
            }

            return setting;
        }
    }
}
